//
//  ActivityCenterViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/16.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ActivityCenterViewController.h"
#import "HotActivityData.h"
#import "HotActivityCell.h"
@interface ActivityCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation ActivityCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource=[NSMutableArray array];
    [self setUpTableView];
    [self getHotData];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    
}
#pragma mrak---表
-(void)setUpTableView
{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=150;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"HotActivityCell" bundle:nil] forCellReuseIdentifier:@"hot"];
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 10;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotActivityCell *cell=[_tableView dequeueReusableCellWithIdentifier:@"hot" forIndexPath:indexPath];
    HotActivityData *data=self.dataSource[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:data.appIndexImage] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.time.text=data.RemainderTime;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.peopleNumber.text=[NSString stringWithFormat:@"已有%ld人参与",(long)data.ParticipateNum];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotActivityData *data=self.dataSource[indexPath.row];
    if (data.IsStatic==1) {
        WebViewController *webView=[[WebViewController alloc]init];
        webView.title=data.Title;
        webView.pageUrl=data.PageUrl;
        [self.navigationController pushViewController:webView animated:YES];
    }
}
#pragma mark--获取网络数据
-(void)getHotData
{
    //http://192.168.1.107:3722/Activity/ActList/{UserCode/{VerNumber}/{CityCode}/{ClientType}/{IsHot}/{Timestamp}/{Sign}
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *signDic=@{@"UserCode":GETUSERCODE,@"VerNumber":VERNUMBER,@"CityCode":GETCITYCODE,@"ClientType":CLIENTTYPE,@"IsHot":@"0",@"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:signDic];
    NSString *urlString=[NSString stringWithFormat:@"Activity/ActList/%@/%@/%@/%@/%@/%@/%@",GETUSERCODE,VERNUMBER,GETCITYCODE,CLIENTTYPE,@"0",timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error)
        {
            [MBProgressHUD showError:@"网络请求超时"];
        }else
        {
            if ([responses[@"IsSuccess"]intValue]==1)
            {
                [MBProgressHUD hideHUDForView:self.view];
                NSDictionary *dic=responses[@"ReturnObject"];
                NSArray *keys=[dic allKeys];
                for (NSString *str in keys)
                {
                    if ([str isEqualToString:@"HotActList"])
                    {
                        NSArray *dataArr=dic[@"HotActList"];
                        for (NSDictionary *datadic in dataArr)
                        {
                            HotActivityData *data=[[HotActivityData alloc]initWithDic:datadic];
                            [self.dataSource addObject:data];
                        }
                        [_tableView reloadData];
                    }
                }
            }
        }
    }];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
